package visualizer.client.utils;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import visualizer.client.event.ResizeEventHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * User: DDovzhenko
 * Date: 28.12.13
 * Time: 18:35
 */
public class ScaleInfo {
    private static final double DEFAULT_WIDTH = 1280;
    private static final double DEFAULT_HEIGHT = 800;
    private static ScaleInfo instance = new ScaleInfo();

    private List<ResizeEventHandler> handlers = new ArrayList<ResizeEventHandler>();
    private double scale;

    public ScaleInfo() {
        calculateScale();

        Window.addResizeHandler(new ResizeHandler() {
            @Override
            public void onResize(ResizeEvent event) {
                calculateScale();
                for (ResizeEventHandler handler : handlers) {
                    handler.onResize();
                }
            }
        });
    }

    public void addResizeEventHandler(ResizeEventHandler handler) {
        this.handlers.add(handler);
    }

    private void calculateScale() {
        double scaleX = Window.getClientWidth() / DEFAULT_WIDTH;
        double scaleY = Window.getClientHeight() / DEFAULT_HEIGHT;
        if (scaleY <= scaleX) {
            scale = scaleY;
        } else {
            scale = scaleX;
        }
    }

    public double getScale() {
        return scale;
    }

    public String getMozTransformScale() {
        return "scale(" + scale + ", " + scale + ")";
    }

    public static ScaleInfo getInstance() {
        return instance;
    }
}
